/// @tags:
#include <cstdio>
#include <iostream>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int t = 0;
  cin >> t;
  while (t--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a == 0 && c == 0) {
      cout << 0 << endl;
      continue;
    }
    if (a == 0 || c == 0) {
      cout << 1 << endl;
      continue;
    }
    int gcd1 = gcd(a, b);
    a /= gcd1, b /= gcd1;
    int gcd2 = gcd(c, d);
    c /= gcd2, d /= gcd2;
    if (a == c && b == d) {
      cout << 0 << endl;
      continue;
    }
    if (1.0 * a / b > 1.0 * c / d) {
      std::swap(a, c), std::swap(b, d);
    }
    if (1ll * c * b % (1ll * a * d) == 0) {
      cout << 1 << endl;
      continue;
    }
    cout << 2 << endl;
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("A_Burenka_Plays_with_Fractions.in", "r", stdin);
  freopen("A_Burenka_Plays_with_Fractions.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
